﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AdminControl_EditProDetails : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String proid = Request["proid"];
        ShoppingDataContext spdt = new ShoppingDataContext();

        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("imageurl");
        dataTable.Columns.Add("proname");
        dataTable.Columns.Add("descript");
        dataTable.Columns.Add("proid");

        var prodetails = from p in spdt.PRODUCTs
                         where p.ProductID == proid
                         select p;

        if (prodetails.Count() <= 0) return;
        DataRow row = dataTable.NewRow();
        row["proname"] = prodetails.First().ProName;
        row["imageurl"] = prodetails.First().image;
        row["descript"] = prodetails.First().Descript;
        row["proid"] = proid;

        dataTable.Rows.Add(row);
        productdetails.DataSource = dataTable;
        productdetails.DataBind();
    }

    protected void DataBound(object sender, DataListItemEventArgs e)
    {
        DropDownList usefors = (DropDownList)e.Item.FindControl("usefor");
        DropDownList types = (DropDownList)e.Item.FindControl("type");
        DropDownList genders = (DropDownList)e.Item.FindControl("gender");
        String proid = ((HiddenField)e.Item.FindControl("proid")).Value;
        ShoppingDataContext spdt = new ShoppingDataContext();

        var usefor_ele = from p in spdt.USEFORs
                         select p;
        foreach (var ele in usefor_ele)
        {
            usefors.Items.Add(ele.usefor1);
        }

        var type_ele = from p in spdt.TYPEs
                         select p;
        foreach (var ele in type_ele)
        {
            types.Items.Add(ele.type1);
        }

        var gender_ele = from p in spdt.GENDERs
                         select p;
        foreach (var ele in gender_ele)
        {
            genders.Items.Add(ele.gender1);
        }
        
        var selectpros = from p in spdt.PRODUCTs
                        where p.ProductID == proid
                         select p;

        if (selectpros.Count() <= 0) return;

        usefors.SelectedValue = selectpros.First().usefor;
        types.SelectedValue = selectpros.First().type;
        genders.SelectedValue = selectpros.First().gender;

    }
   
    protected void save_Click(object sender, EventArgs e)
    {
        Button save = (Button)sender;
        DataListItem dt = (DataListItem)save.Parent;
        String proname = ((TextBox)dt.FindControl("proname")).Text;
        String usefor = ((DropDownList)dt.FindControl("usefor")).SelectedValue;
        String type = ((DropDownList)dt.FindControl("type")).SelectedValue;
        String gender = ((DropDownList)dt.FindControl("gender")).SelectedValue;
        String descript = ((TextBox)dt.FindControl("descript")).Text;
        String proid = ((HiddenField)dt.FindControl("proid")).Value;

        ShoppingDataContext spdt = new ShoppingDataContext();
        PRODUCT pro = spdt.PRODUCTs.Single(p => p.ProductID == proid);
        pro.ProName = proname;
        pro.usefor = usefor;
        pro.type = type;
        pro.gender = gender;
        pro.Descript = descript;
        spdt.SubmitChanges();
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}
